﻿using DetectScars.model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DetectScars.Menus
{
    public partial class WorkPiece : Form
    {
        public WorkPiece()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }
        internal WorkPiece(Piece piece_)
        {
            InitializeComponent();
            piece = piece_;
            this.StartPosition = FormStartPosition.CenterScreen;
        }
        Piece piece;

        internal Piece GetPiece()
        {
            return piece;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            piece = new Piece();
            piece.Name = textBox1.Text;
            piece.InnerDiameter_Min = (float)numericUpDown1.Value;
            piece.InnerDiameter_Max = (float)numericUpDown5.Value;
            piece.OutsideDiameter_Min = (float)numericUpDown2.Value;
            piece.OutsideDiameter_Max = (float)numericUpDown6.Value;
            piece.JCWZ = comboBox1.Text;
            piece.Height = (float)numericUpDown3.Value;
            piece.Weight = (float)numericUpDown4.Value;
            if (piece.Name.Trim() == "")
            {
                MessageBox.Show("请输入零件编号");
                return;
            }
            if (piece.InnerDiameter_Min == 0 || piece.InnerDiameter_Max == 0 || piece.OutsideDiameter_Max == 0 || piece.OutsideDiameter_Min == 0 || piece.Height == 0)
            {
                MessageBox.Show("数值不可为0");
                return;
            }
            if (piece.InnerDiameter_Min > piece.InnerDiameter_Max)
            {
                MessageBox.Show("内小直径不能大于内大直径");
                return;
            }
            if (piece.OutsideDiameter_Min > piece.OutsideDiameter_Max)
            {
                MessageBox.Show("外小直径不得大于外大直径");
                return;
            }
            if (piece.InnerDiameter_Max >= piece.OutsideDiameter_Min)
            {
                MessageBox.Show("外直径不得小于内直径");
                return;
            }
            piece.Id = Guid.NewGuid().ToString();
            DialogResult = DialogResult.OK;
        }

        private void WorkPiece_Load(object sender, EventArgs e)
        {
            if (piece != null)
            {
                textBox1.Text = piece.Name;
                numericUpDown1.Value = (decimal)piece.InnerDiameter_Min;
                numericUpDown2.Value = (decimal)piece.OutsideDiameter_Min;
                numericUpDown3.Value = (decimal)piece.Height;
                numericUpDown4.Value = (decimal)piece.Weight;
                numericUpDown5.Value = (decimal)piece.InnerDiameter_Max;
                numericUpDown6.Value = (decimal)piece.OutsideDiameter_Max;
                comboBox1.Text = piece.JCWZ;
                button1.Text = "修改";
            }
            else
            {
                button1.Text = "保存";
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
